package cn.china.xfood.controller;

import cn.china.xfood.service.*;

import com.alibaba.fastjson.JSON;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    ProductService productService;

    @Autowired
    UserorderService userorderService;

    @Autowired
    WebinfoService webinfoService;

    @Autowired
    UsermenuService usermenuService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request, HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin","*");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = new User();
        user.setUsername(username);user.setPassword(password);
        User reUser = userService.login(user);




        if(reUser!=null){//1 账号正确 查到了
            if(username.equals(reUser.getUsername())&&password.equals(reUser.getPassword())){
                //2 用户名或者密码正确

                return "yes";
            }else{
                // 3 用户名或者密码错误
                return "error";
            }

        }else{
            //1 账号不正确  查不到
            return "no";
        }


//        if(reUser==null){
//            return "no";
//        }else if(username.equals(reUser.getUsername())){
//            return "";
//        }   else{
//            return "yes";
//        }


    }


    @RequestMapping("/register")
    public String register(HttpServletRequest request,HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin","*");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");

        User user = new User();user.setUsername(username);user.setPassword(password);user.setPhone(phone);
        User reuser = userService.login(user);
        if(reuser!=null&&reuser.getUsername().equals(username)){//用户已存在
            return "1";
        }else{
            //不存在
          int i = userService.insertSelective(user);
          if(i>0){
              return "ok";
          }else{
              return "no";
          }
        }
    }

    @RequestMapping("/getWebData")
    public List<Webinfo> getWebData(HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin","*");
        List<Webinfo> webinfos = webinfoService.selectAll();
        return  webinfos;
    }


    @RequestMapping("/getUserOrder")
    public Map<String,Object> getUserOrder(HttpServletRequest request,HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin","*");
        int pageCount = Integer.parseInt(request.getParameter("limit"));
        int pageStr = Integer.parseInt(request.getParameter("page"));
        String username = request.getParameter("username");
        int currentPage = (pageStr-1)*pageCount;

        List<Userorder> userOrder = userorderService.getUserOrder(username,currentPage, pageCount);
        int i = userorderService.getOrderCount(username);

        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",i);
        map.put("data",userOrder);
        String s = JSON.toJSONString(map);
        return map;

    }

    @RequestMapping("/getUserMenu")
    public Map<String,Object> getUsermenu(@RequestParam String limit,@RequestParam String page,@RequestParam String orderNumber,HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin","*");
        int pageCount = Integer.parseInt(limit);
        int pageNum = Integer.parseInt(page);
        int currentPage = (pageNum-1)*pageCount;

        List<Usermenu> userMenu = usermenuService.getUserMenu(orderNumber, currentPage, pageCount);
        List<Usermenub> usermenubs  = new ArrayList<>();
        for (int i = 0; i < userMenu.size(); i++) {
            Usermenu usermenu = userMenu.get(i);
            String ordernumber = usermenu.getOrderNumber();
            String productName = productService.selectByPrimaryKey(usermenu.getProductId()).getProductName();
            Integer productAmount = usermenu.getProductAmount();
            Double productPrice = usermenu.getProductPrice();

            usermenubs.add(new Usermenub(ordernumber,productName,productAmount,productPrice));
        }

        int  i = usermenuService.getMenuCount(orderNumber);

        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",i);
        map.put("data",usermenubs);
        return map;
    }

    @RequestMapping("/getAllOrder")
    public Map<String,Object> getAllOrder(@RequestParam String limit,@RequestParam String page,HttpServletResponse res ){
        res.setHeader("Access-Control-ALLow-Origin","*");
        int pageCount = Integer.parseInt(limit);
        int pageNum = Integer.parseInt(page);
        int currentPage = (pageNum-1)*pageCount;
        System.out.println("limit:"+limit);
        System.out.println("page:"+page);

        List<Userorder> allOrder = userorderService.getAllOrder(currentPage, pageCount);
        int i = userorderService.getAllOrderCount();

        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",i);
        map.put("data",allOrder);
        return map;
    }


}
